package com.huluobo.a2203kotlinshopping.cart

import androidx.lifecycle.MutableLiveData
import com.google.gson.Gson
import com.huluobo.base.bean.CartBeanResult
import com.huluobo.base.bean.ChangeCartBeanResult
import com.huluobo.base.net.Const
import com.huluobo.base.viewModel.BaseViewModel
import okhttp3.MediaType
import okhttp3.RequestBody

/**
 *  Created by LC on 2024/6/26.
 */
class CartViewModel : BaseViewModel<CartRepo>() {

    val queryCartSuccess = MutableLiveData<CartBeanResult>()
    val queryCartFaield = MutableLiveData<String>()

    val deleteCartSuccess = MutableLiveData<ChangeCartBeanResult>()
    val deleteCartFailed = MutableLiveData<String>()

    val changeCartSuccess = MutableLiveData<ChangeCartBeanResult>()
    val changeCartFailed = MutableLiveData<String>()

    fun deleteCart(ids: String) {
        val map = mutableMapOf<String, String>()
        map[Const.PARAM_IDS] = ids

        val json = Gson().toJson(map)
        val body = RequestBody.create(MediaType.parse(Const.MEDIA_TYPE), json)
        repo.deleteCart(body, deleteCartSuccess, deleteCartFailed)
    }

    fun changeCartCount(count: Int, goodsId: Int) {
        val map = mutableMapOf<String, Int>()
        map[Const.PARAM_COUNT] = count
        map[Const.PARAM_GOODS_ID] = goodsId
        val json = Gson().toJson(map)
        val body = RequestBody.create(MediaType.parse(Const.MEDIA_TYPE), json)
        repo.changeCartCount(body, changeCartSuccess, changeCartFailed)
    }

    fun queryCart() {
        repo.queryCart(queryCartSuccess, queryCartFaield)
    }

    override fun createRepo(): CartRepo = CartRepo()
}